/////precompiled////////
#include "precompiled.h"
////////////////////////


#include "prs_Image.h"

using namespace prs;

//////////TNodeFrmImages/////////////////
grf::TImageGdiP *TNodeImages::GetImage(const svl::TString &Name,svl::TString &OutError)
{
  TNodeArray arr;
  SearchByAttrNameAndValue("id", Name, arr );
  TNodeImage *P;
  if ( arr.Count() > 0 ) {
     P = dynamic_cast<TNodeImage*>( &arr.Get(0) ); 
    if (!P) {
      Except("SearchImageById[!P]");
    } 
    return &P->Image;
  }
  else{
    P = new TNodeImage;
	svl::TString FileMask = Path + Name + ".*";
	WIN32_FIND_DATA wfd;
	if( FindFirstFile( FileMask, &wfd ) != INVALID_HANDLE_VALUE ){
		if( !P->LoadImage( Path + wfd.cFileName )){
			delete P;
			return NULL;
		//OutError = Name + " not found.";
		//::MessageBox( NULL, Name + " not found.", "Missing image", MB_OK ); 
		//
		//if( Name == "dummy" )
		//	Except( " Missing dummy image." );
		//P = GetImage("dummy");
		}
	}
	else{
		delete P;
		return NULL;
	}
	P->Id.SetValue( Name );
	Add(P,true);
	return &P->Image;
  }
  return NULL;
}

// TNodeImage ////////////////////////////////////////////////////

int TNodeImage::LoadImage( const svl::TString &Name ){
	Image.LoadFromFile( Name );
	if( (Image.Width() == 0) && (Image.Height() == 0) )
		return 0;
	return 1;
}